﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using ICDA.Web.Models;
using ICDA.Models;
using ICDA.Repositories.ICDA;
using System.Data.Entity;
using ICDA.Models.Entity;

namespace ICDA.Web.Controllers
{
    public class AccountController : Controller
    {
        private static IRepository _repository;
        private static UserModel _userModel;
        public static UserModel userModel
        {
            get
            {
                if (_userModel == null)
                {
                    _userModel = new UserModel();
                }
                return _userModel;
            }
        }
        public static IRepository repository
        {
            get
            {
                if (_repository == null)
                {
                    _repository = new Repository();
                }
                return _repository;
            }
        }
        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = repository.users.FirstOrDefault(u => u.UserID.Trim() == model.UserName.Trim() && u.Status == true);
                if (user.Password.Trim() == model.Password.Trim())
                {
                    Session["UserName"] = user.Name;
                    Session["UID"] = user.UserID;
                    Session["user"] = user;
                    Session["Role"] = user.Role;
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return View();
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            Session.Clear();
            return RedirectToAction("LogOn", "Account");
        }

        public ActionResult MyAccount()
        {
            if (Session["UID"] != null)
            {
                string uid = Session["UID"].ToString().Trim();
                UserTable user = _repository.users.Where(u => u.UserID == uid).FirstOrDefault();
                if (user != null)
                {
                    userModel.Name = user.Name;
                    userModel.EmailID = user.EmailID;
                    userModel.SkillSets = user.Skills;
                    userModel.TeamName = user.TeamName;
                }
                return View(userModel);
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }
        public ActionResult ChangePassword()
        {
            if (Session["UID"] != null)
            {
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangePassword(ChangePasswordModel viewModel)
        {
            string uid = Session["UID"].ToString().Trim();
            UserTable user = _repository.users.Where(u => u.UserID == uid).FirstOrDefault();
            if (user.Password == viewModel.OldPassword)
            {
                user.Password = viewModel.NewPassword;
            }
            _repository.ChangePassword(user);
            _repository.SaveChanges();
            return View();
        }

        public ActionResult EditMyInfo()
        {
            if (Session["UID"] != null)
            {
                string uid = Session["UID"].ToString().Trim(); ;
                UserTable user = _repository.users.Where(u => u.UserID == uid).FirstOrDefault();
                if (user != null)
                {
                    userModel.Name = user.Name;
                    userModel.EmailID = user.EmailID;
                    userModel.SkillSets = user.Skills;
                    userModel.TeamName = user.TeamName;
                }
                return View(userModel);
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditMyInfo(UserModel viewModel)
        {
            string uid = Session["UID"].ToString().Trim();
            UserTable user = _repository.users.Where(u => u.UserID == uid).FirstOrDefault();       
            user.Name = viewModel.Name;
            user.EmailID = viewModel.EmailID;
            user.Skills = viewModel.SkillSets;
            user.TeamName = viewModel.TeamName;
            _repository.UpdateUser(user);
            if (_repository.SaveChanges())
            {
                return RedirectToAction("MyAccount", "Home");
            }
            else
            {
                return View(viewModel);
            }
        }
    }
}
